(function webpackUniversalModuleDefinition(root, factory) {

	if (typeof exports === 'object' && typeof module === 'object') {
		module.exports = factory();
	}

	else if (typeof define === 'function' && define.amd) {
		define([], factory);
	}

	else if (typeof exports === 'object') {
		exports["KProfile"] = factory();
	}

	else{
		debugger
		root["KProfile"] = factory();
	}

})(window, function () {
	return (function (modules) { // webpackBootstrap
		// The module cache
		var installedModules = {};

		// The require function
		function __webpack_require__(moduleId) {

			// Check if module is in cache
			if (installedModules[moduleId]) {
				return installedModules[moduleId].exports;

			}
			// Create a new module (and put it into the cache)
			var module = installedModules[moduleId] = {
				i: moduleId,
				l: false,
				exports: {}

			};

			// Execute the module function
			modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

			// Flag the module as loaded
			module.l = true;

			// Return the exports of the module
			return module.exports;

		}


		// expose the modules object (__webpack_modules__)
		__webpack_require__.m = modules;

		// expose the module cache
		__webpack_require__.c = installedModules;

		// define getter function for harmony exports
		__webpack_require__.d = function (exports, name, getter) {
			if (!__webpack_require__.o(exports, name)) {
				Object.defineProperty(exports, name, { enumerable: true, get: getter });

			}

		};

		// define __esModule on exports
		__webpack_require__.r = function (exports) {
			if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });

			}
			Object.defineProperty(exports, '__esModule', { value: true });

		};

		// create a fake namespace object
		// mode & 1: value is a module id, require it
		// mode & 2: merge all properties of value into the ns
		// mode & 4: return value when already ns object
		// mode & 8|1: behave like require
		__webpack_require__.t = function (value, mode) {
			if (mode & 1) value = __webpack_require__(value);
			if (mode & 8) return value;
			if ((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
			var ns = Object.create(null);
			__webpack_require__.r(ns);
			Object.defineProperty(ns, 'default', { enumerable: true, value: value });
			if (mode & 2 && typeof value != 'string') for (var key in value) __webpack_require__.d(ns, key, function (key) { return value[key]; }.bind(null, key));
			return ns;

		};

		// getDefaultExport function for compatibility with non-harmony modules
		__webpack_require__.n = function (module) {
			var getter = module && module.__esModule ?
				function getDefault() { return module['default']; } :
				function getModuleExports() { return module; };
			__webpack_require__.d(getter, 'a', getter);
			return getter;

		};

		// Object.prototype.hasOwnProperty.call
		__webpack_require__.o = function (object, property) { return Object.prototype.hasOwnProperty.call(object, property); };

		// __webpack_public_path__
		__webpack_require__.p = "";

		debugger
		// Load entry module and return exports
		return __webpack_require__(__webpack_require__.s = "./kProfile-media.js");

	})
		/************************************************************************/
		({

/***/ "./kProfile-media.js":
/*!***************************!*\
  !*** ./kProfile-media.js ***!
  \***************************/
/*! exports provided: default */
/***/ (function (module, __webpack_exports__, __webpack_require__) {

					"use strict";
					__webpack_require__.r(__webpack_exports__);
					// 默认配置
					const testConfig = {
						path: 'http://10.68.8.132:5001/resource',
						minFps: 23,
						saveMinute: 10,
						testNum: 24,
						testType: 'webrtc',
						mp4ToWebrtc: 1.5,
						playTime: 1000,
						maxTestTime: 30 * 1000
					};
					let kmedias = [], timer = null, callbackMap = {}, busyArr = [], freeArr = [], container, containerStyle, isTesting = false,
						profileContainer, profileContainerStyle;

					// 计算1s内的fps
					const simulator = async (timeout = 1000) => {
						let frame = 0;
						let lastTime = performance.now();
						// let cpuAll = 0;
						let result = {};
						const loop = () => {
							return new Promise(async (resolve) => {
								let now = performance.now();
								if (now >= timeout + lastTime) {
									let fps = parseInt((frame * 1000) / (now - lastTime));
									let t = performance.timing;
									let memory = 0;
									if (t = performance.memory) {
										memory = (t.usedJSHeapSize / t.totalJSHeapSize * 100).toFixed(2) + '%';
									}
									result.fps = fps;
									result.memory = memory;
									resolve(result);
								} else {
									window.requestAnimationFrame(async () => {
										frame++;
										let result = await loop();
										resolve(result);
									});
								}
							});
						};
						await loop();
						return result;
					};
					const loadStyleString = (css) => {
						let style = document.createElement('style');
						style.type = 'text/css';
						try {
							style.appendChild(document.createTextNode(css));
						} catch (ex) {
							style.styleSheet.cssText = css;
						}
						let head = document.getElementsByTagName('head')[0];
						head.appendChild(style);
						return style;
					};
					// 创建性能测试容器
					const createTestContainer = () => {
						containerStyle = loadStyleString(`
          .kprofile-test-container:before{content: '';
          position: absolute;left: 0; top: 0; right:0; bottom:0;
          background: black;z-index: 9;
          }
          .kprofile-scene {
            width: 500px;
            height: 500px;
            -webkit-perspective: 600;
                    perspective: 600;
                    text-align: center;
          }
          .kprofile-scene svg {
            width: 240px;
            height: 240px;
          }

          .dc-logo {
            position: fixed;
            right: 10px;
            bottom: 10px;
          }

          .dc-logo:hover svg {
            -webkit-transform-origin: 50% 50%;
                    transform-origin: 50% 50%;
            -webkit-animation: arrow-spin 2.5s 0s cubic-bezier(0.165, 0.84, 0.44, 1) infinite;
                    animation: arrow-spin 2.5s 0s cubic-bezier(0.165, 0.84, 0.44, 1) infinite;
          }
          .dc-logo:hover:hover:before {
            content: "";
            padding: 6px;
            font: 10px/1 Monaco, sans-serif;
            font-size: 10px;
            color: #00fffe;
            text-transform: uppercase;
            position: absolute;
            left: -70px;
            top: -30px;
            white-space: nowrap;
            z-index: 20px;
            box-shadow: 0px 0px 4px #222;
            background: rgba(0, 0, 0, 0.4);
          }
          .dc-logo:hover:hover:after {
            content: 'Digital Craft';
            padding: 6px;
            font: 10px/1 Monaco, sans-serif;
            font-size: 10px;
            color: #6E6F71;
            text-transform: uppercase;
            position: absolute;
            right: 0;
            top: -30px;
            white-space: nowrap;
            z-index: 20px;
            box-shadow: 0px 0px 4px #222;
            background: rgba(0, 0, 0, 0.4);
            background-image: none;
          }

          @-webkit-keyframes arrow-spin {
            50% {
              -webkit-transform: rotateY(360deg);
                      transform: rotateY(360deg);
            }
          }

          @keyframes arrow-spin {
            50% {
              -webkit-transform: rotateY(360deg);
                      transform: rotateY(360deg);
            }
          }
          `);
						container = document.createElement('div');
						container.className = 'kprofile-test-container';
						container.innerHTML = `<div class="kprofile-scene" style="position: fixed;top: 50%; left: 50%; transform: translate(-50%, -50%);z-index: 99;">
          <svg
          version="1.1"
          id="dc-spinner"
          xmlns="http://www.w3.org/2000/svg"
          x="0px" y="0px"
          width:"38"
          height:"38"
          viewBox="0 0 38 38"
          preserveAspectRatio="xMinYMin meet"
          >
          <text x="12" y="21" font-family="Monaco" font-size="2px" style="letter-spacing:0.6" fill="grey">性能检测中...
          </text>
          <path fill="#373a42" d="M20,35c-8.271,0-15-6.729-15-15S11.729,5,20,5s15,6.729,15,15S28.271,35,20,35z M20,5.203
            C11.841,5.203,5.203,11.841,5.203,20c0,8.159,6.638,14.797,14.797,14.797S34.797,28.159,34.797,20
            C34.797,11.841,28.159,5.203,20,5.203z">
          </path>

          <path fill="#373a42" d="M20,33.125c-7.237,0-13.125-5.888-13.125-13.125S12.763,6.875,20,6.875S33.125,12.763,33.125,20
            S27.237,33.125,20,33.125z M20,7.078C12.875,7.078,7.078,12.875,7.078,20c0,7.125,5.797,12.922,12.922,12.922
            S32.922,27.125,32.922,20C32.922,12.875,27.125,7.078,20,7.078z">
          </path>

          <path fill="#2AA198" stroke="#2AA198" stroke-width="0.6027" stroke-miterlimit="10" d="M5.203,20
              c0-8.159,6.638-14.797,14.797-14.797V5C11.729,5,5,11.729,5,20s6.729,15,15,15v-0.203C11.841,34.797,5.203,28.159,5.203,20z">
          <animateTransform
                attributeName="transform"
                type="rotate"
                from="0 20 20"
                to="360 20 20"
                calcMode="spline"
                keySplines="0.4, 0, 0.2, 1"
                keyTimes="0;1"
                dur="2s"
                repeatCount="indefinite" />
           </path>

          <path fill="#859900" stroke="#859900" stroke-width="0.2027" stroke-miterlimit="10" d="M7.078,20
          c0-7.125,5.797-12.922,12.922-12.922V6.875C12.763,6.875,6.875,12.763,6.875,20S12.763,33.125,20,33.125v-0.203
          C12.875,32.922,7.078,27.125,7.078,20z">
           <animateTransform
              attributeName="transform"
              type="rotate"
              from="0 20 20"
              to="360 20 20"
              dur="1.8s"
              repeatCount="indefinite" />
            </path>
          </svg>
          <div id="kprofile-test-explain">
          </div>
        </div>
        `;
						container.style = 'position: fixed;right: 0;bottom: 0;left: 0; top: 0;z-index: 999;overflow:hidden;';
						document.body.appendChild(container);
					};
					// 销毁性能测试创建容器
					const removeTestContainer = () => {
						kmedias.map(k => k.closeMedia());
						container && container.remove();
						containerStyle && containerStyle.remove();
					};
					// 测试项集合
					const TestItem = {
						CreateKMedia: (createConfig, n = 0) => {
							let kmedia;
							let selector = document.createElement('div');
							selector.style.width = '200px';
							selector.style.height = '100px';
							selector.style.display = 'inline-block';
							container.appendChild(selector);
							return new Promise(r => {
								let config = {
									selector: selector,
									mediaUrl: createConfig.path + `/${n + 1}.mp4`,
									mediaType: 'video',
									muted: true,
									autoplay: true,
									tools: ['play', 'timings', 'videoRecorder', 'volume', 'requestFullscreen'],
								};
								kmedia = new createConfig.KMedia(config);
								kmedia.onPlay(function () { r() });
								kmedias.push(kmedia);
							});
						}
					};
					// 创建测试项
					const createTestItem = (item) => {
						if (item.KMedia) {
							return TestItem.CreateKMedia;
						}
					};

					// 取缓存
					const getCache = (config) => {
						config = {
							saveMinute: testConfig.saveMinute,
							...config
						};
						let testEnv = () => {
							let start = performance.now();
							for (let i = 0; i < 100000; i++) { for (let j = 0; j < 10000; j++) { } };
							return performance.now() - start;
						}
						let cache = {
						};
						let envWasteTime = testEnv();
						cache.envWasteTime = envWasteTime;
						let profileResults = localStorage.getItem('profileResults');
						cache.results = profileResults ? JSON.parse(profileResults) : {};
						if (envWasteTime >= 2500) {
							cache.score = 0;
						} else if (envWasteTime >= 2000) {
							cache.score = 1;
						} else if (envWasteTime >= 1500) {
							cache.score = 2;
						}
						if (profileResults) {
							let results = cache.results;
							let resKeys = Object.keys(results);
							for (let i = 0; i < resKeys.length; i++) {
								let lastEnvWasteTime = resKeys[i];
								let result = results[lastEnvWasteTime];
								if ((new Date().getTime() - result.testTime) >= config.saveMinute * 1000 * 60) {
									results[resKeys] = undefined;
									delete results[lastEnvWasteTime];
									localStorage.setItem('profileResults', JSON.stringify(results));
									continue;
								}
								if (Math.abs(envWasteTime - lastEnvWasteTime) <= 400) {
									cache.score = results[lastEnvWasteTime].lushu;
									return cache;
								}
							}
						}
						return cache;
					};

					// 递归测试
					const testLoop = (config, lushu = 0, startTime = performance.now()) => {
						let playTime = config.playTime;
						let testMethod = createTestItem(config);
						return new Promise(async r => {
							let fpsInfo = await kProfile.getSystemInfo(playTime);
							let mainFunc = async () => {
								if (lushu >= config.testNum || fpsInfo.fps < config.minFps || (performance.now() - startTime >= config.maxTestTime)) {
									r(lushu);
								} else {
									await testMethod(config, lushu);
									lushu++;
									r(await testLoop(config, lushu, startTime));
								}
							};
							mainFunc();
						});
					};

					// 性能分析
					const analyse = async (config, cache) => {
						let { results, envWasteTime } = cache;
						createTestContainer();
						let lushu = await testLoop(config);
						if (config.testType == 'webrtc') {
							lushu = parseInt(lushu / testConfig.mp4ToWebrtc);
						}
						results[envWasteTime] = {
							lushu: lushu,
							type: 'kmeida',
							testTime: new Date().getTime()
						}
						localStorage.setItem('profileResults', JSON.stringify(results));
						return lushu;
					};

					const testSystem = async (config) => {
						isTesting = true;
						config = {
							...testConfig,
							...config
						};
						try {
							if (document) {
								let cache = getCache(config);
								if (cache.score !== undefined) {
									return cache.score;
								} else {
									return await analyse(config, cache);
								}
							} else {
								throw ('请在浏览器环境执行');
							}
						} catch (err) {
							alert('测试出了点问题，请重新进行测试');
						} finally {
							removeTestContainer();
							isTesting = false;
						}
					};
					const KProfile = {
						version: '0.0.5',
						testSystem: testSystem,
						checkReTest: async (testConfig) => {
							let { score } = getCache();
							if (score != undefined) {
								return score;
							} else {
								return await testSystem(testConfig);
							}
						},
						getSystemInfo: async (timeout) => {
							let { fps, memory } = await simulator(timeout);
							return {
								fps,
								memory
							};
						},
						watch: (callbacks, isView = true) => {
							if (isView) {
								profileContainer = document.getElementById('kprofile_contaner');
								if (!profileContainer) {
									profileContainerStyle = loadStyleString(`#kprofile_contaner {
          box-sizing: content-box;
          width: 100px;
          height: 100px;
          border-radius: 50%;
          border: 3px solid #67c23a;
          background: #ffffff;
          overflow: hidden;
          position: absolute;
          bottom: 20px;
          right: 20px;
          padding: 5px;
        }
        #kprofile_contaner .wave {
          position: relative;
          width: 100px;
          height: 100px;
          background-image: linear-gradient(-180deg, #aaff80 13%, #67c23a 91%);
          border-radius: 50%;
        }
        #kprofile_contaner .wave-mask {
          position: absolute;
          width: 200px;
          height: 200px;
          top: 0;
          left: 50%;
          border-radius: 40%;
          background-color: rgba(255, 255, 255, 0.9);
          transform: translate(-50%, -70%) rotate(0);
          animation: toRotate 10s linear -5s infinite;
          z-index: 20;
        }
        #kprofile_contaner.warning {
          border: 3px solid #e6a23c;
        }
        #kprofile_contaner.danger {
          border: 3px solid #f56c6c;
        }
        #kprofile_contaner.warning .wave {
          background-image: linear-gradient(-180deg, #f0c78a 13%, #e6a23c 91%);
        }
        #kprofile_contaner.danger .wave {
          background-image: linear-gradient(-180deg, #f78989 13%, #f56c6c 91%);
        }
        #kprofile_contaner #value {
          z-index: 21;
          position: absolute;
          width: 100%;
          left: 0;
          top: 0;
          bottom: 0;
          color: #67c23a;
          text-align: center;
          line-height: 110px;
        }
        #kprofile_contaner.warning #value {
          color: #e6a23c;
        }
        #kprofile_contaner.danger #value {
          color: #f56c6c;
        }
        @keyframes toRotate {
          50% {
            transform: translate(-50%, -70%) rotate(180deg);
          }
          100% {
            transform: translate(-50%, -70%) rotate(360deg);
          }
        }`);
									let fragment = document.createDocumentFragment();
									profileContainer = document.createElement('div');
									profileContainer.id = 'kprofile_contaner';
									profileContainer.innerHTML = ` <div id="value"></div><div class="wave"></div>
        <div class="wave-mask" id="kprofile_wave_mask"></div>`;
									fragment.appendChild(profileContainer);
									document.body.appendChild(fragment);
								}
							}
							callbackMap['free'] = callbacks.freeCallback;
							callbackMap['busy'] = callbacks.busyCallback;
							if (!timer) {
								timer = async () => {
									let res = await kProfile.getSystemInfo(1000);
									if (res.fps >= 48) {
										busyArr = [];
										if (res.fps >= 60) {
											freeArr.push(res.fps);
											if (freeArr.length >= 6) {
												callbackMap['free'] && callbackMap['free']();
												freeArr = [];
											}
										} else {
											freeArr = [];
										}
									} else {
										freeArr = [];
										if (res.fps <= 24) {
											busyArr.push(res.fps);
											if (busyArr.length >= 2) {
												callbackMap['busy'] && callbackMap['busy']();
												busyArr = [];
											}
										} else {
											busyArr = [];
										}
									}
									if (isView) {
										let fps = res.fps
										profileContainer.querySelector('#value').innerHTML = fps;
										profileContainer.querySelector('#kprofile_wave_mask').style.top = 40 - parseInt((1 - fps / 60) * 100) + 'px'
										if (res.fps <= 24) {
											profileContainer.className = 'danger';
										} else if (res.fps <= 42) {
											profileContainer.className = 'warning';
										} else {
											profileContainer.className = '';
										}
									}
									if (timer) {
										await timer();
									}
								}
								timer();
							}
						},
						removeWatch: () => {
							profileContainer && profileContainer.remove();
							profileContainerStyle && profileContainerStyle.remove();
							callbackMap = {};
							timer = null;
						}
					};

/* harmony default export */ __webpack_exports__["default"] = KProfile;


					/***/
				})


		})["default"];
});
